package com.tywj.buscustomerapp.model;


import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.model.bean.SearchUtilsBean;
import com.tywj.buscustomerapp.model.bean.TravelLineBean;
import com.tywj.buscustomerapp.presenter.contract.SearchContract;
import com.tywj.buscustomerapp.utils.WebServicrUtils;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class SearchModel implements SearchContract.SearchModel{
    @Override
    public HashMap<SearchUtilsBean,SearchUtilsBean> getStations(String cityId, LatLng start, LatLng end) {
        String stringIo = null;
        List<SearchUtilsBean> beans = null;
        try {
            stringIo = WebServicrUtils.getInstance().getAllStation(cityId);
            InputStream in_nocode = new ByteArrayInputStream(stringIo.getBytes("UTF-8"));
            beans = WebServicrUtils.getInstance().getAddressStations(in_nocode);
            return doGetInfoMessage(beans,start,end);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TravelLineBean> getLineById(String lineId) {
        List<TravelLineBean> beans = null;
        try {
            String stringIo = WebServicrUtils.getInstance().SearchTravelLineInfo(lineId);
            InputStream in_nocode = new ByteArrayInputStream(stringIo.getBytes("UTF-8"));
            beans = WebServicrUtils.getInstance().getLines2(in_nocode);
            return beans;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private HashMap<SearchUtilsBean,SearchUtilsBean> doGetInfoMessage(List<SearchUtilsBean> beans,LatLng start,LatLng end){
        HashMap<SearchUtilsBean,SearchUtilsBean> result = new HashMap<>();
        HashMap<String,SearchUtilsBean> startMap = new HashMap<>();
        HashMap<String,SearchUtilsBean> endMap = new HashMap<>();
        /**
         * 这里获取查询点与各站点距离，取小于等于阈值的站点，
//         * 并取距离最短的站点根据线路编号去重
         */
        for (int i = 0; i < beans.size(); i++) {
//            Log.i("SearchMOdel",beans.get(i).toString());
            LatLng latLng = new LatLng(Double.valueOf(beans.get(i).getLat()),Double.valueOf(beans.get(i).getLng()));
            float distance = AMapUtils.calculateLineDistance(start,latLng);
            if (distance<= Config.MAX_DISTANCE){//这里设置3公里
                beans.get(i).setDistance(distance);
                SearchUtilsBean isBean = startMap.get(beans.get(i).getLinNumber());
                if (isBean!=null){
//                    startMap.put(isBean.getLinNumber(),isBean);
                    if (isBean.getDistance()>beans.get(i).getDistance()){
                        isBean = beans.get(i);
                        startMap.put(isBean.getLinNumber(),isBean);
                    }
                }else{
                    startMap.put(beans.get(i).getLinNumber(),beans.get(i));
                }
            }
            float distance2 = AMapUtils.calculateLineDistance(end,latLng);
            if (distance2<= Config.MAX_DISTANCE){
                beans.get(i).setDistance(distance2);
                SearchUtilsBean isBean = endMap.get(beans.get(i).getLinNumber());
                if (isBean!=null){
                    if (isBean.getDistance()>beans.get(i).getDistance()){
                        isBean = beans.get(i);
                        endMap.put(isBean.getLinNumber(),isBean);
                    }
                }else{
                    endMap.put(beans.get(i).getLinNumber(),beans.get(i));
                }
            }
        }
//        Log.i("SearchMOdelstart",startMap.toString());
//        Log.i("SearchMOdelend",endMap.toString());

        if (start.latitude==end.latitude && start.longitude==end.longitude){
            for (String key:startMap.keySet()) {
                result.put(startMap.get(key),endMap.get(key));
            }
            return result;
        }
        if (startMap.size()>0 && endMap.size()>0){
            for (String key:startMap.keySet()) {
                if (endMap.containsKey(key) && startMap.get(key).getStationId()!=endMap.get(key).getStationId()){//
                    result.put(startMap.get(key),endMap.get(key));
                }
            }
//            Log.i("SearchMOdelend",result.toString());
            return result;
        }else{
//            Log.i("SearchMOdel","null1");
            return null;
        }

    }

    private static double searchArrayMaxMin(List<Double> arry, int begin, int end) {
        double minResult;
        if(end-begin <=1){
            if(arry.get(begin)<arry.get(end)){
                minResult=arry.get(begin);
                return minResult;
            }else{
                minResult= arry.get(end);
                return minResult;
            }
        }
        int mid = (begin+end)/2;
        double result_left = searchArrayMaxMin(arry,begin,mid);
        double result_right =searchArrayMaxMin(arry,mid+1,end);
        if(result_left<result_right)
            minResult= result_left;
        else
            minResult=result_right;
        return minResult;
    }
}
